package com.exam.controller;

import com.exam.entity.StudentScore;
import com.exam.service.StudentScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student-scores")
public class StudentScoreController {

    @Autowired
    private StudentScoreService studentScoreService;

    // 分页查询学生的成绩
    @GetMapping("/{studentId}")
    public Page<StudentScore> getScoresByStudentId(
            @PathVariable Integer studentId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return studentScoreService.getScoresByStudentId(studentId, pageable);
    }

    // 获取学生指定课程的成绩
    @GetMapping("/{studentId}/course")
    public List<StudentScore> getScoresByStudentIdAndCourse(
            @PathVariable Integer studentId,
            @RequestParam String courseName) {
        return studentScoreService.getScoresByStudentIdAndCourse(studentId, courseName);
    }

    // 保存或更新成绩
    @PostMapping
    public StudentScore saveOrUpdateScore(@RequestBody StudentScore studentScore) {
        return studentScoreService.saveOrUpdateScore(studentScore);
    }

    // 删除成绩记录
    @DeleteMapping("/{scoreId}")
    public void deleteScoreById(@PathVariable Integer scoreId) {
        studentScoreService.deleteScoreById(scoreId);
    }
}